#include "header.h"
#include "./Libs/General.h"
#include "./Libs/Calculation.h"
#include "./Libs/MPIWorking.h"

int main(int argc, char *argv[]) {
  double ***Pray, ***Predator;
  int i,j,size,rank;
  int x_points, y_points, from_point, to_point, mode;

  MPI_Init(&argc, &argv);
  MPI_Comm_size(MPI_COMM_WORLD,&size); 
  MPI_Comm_rank(MPI_COMM_WORLD,&rank);

  x_points = get_mynums(size, rank);
  y_points = ysteps;

  from_point = get_fromnum(size, rank);
  to_point = from_point + x_points - 1;
  //printf("%d  %d  %d  %d\n",size,rank,from_point,to_point);

  Pray = InitializeArray(Pray,x_points);
  Predator = InitializeArray(Predator,x_points);
  GetInitialDistribution(Predator[0],Pray[0],from_point-1,to_point+1);
  //Print2dArray(Predator[0], x_points+1, y_points);

  for(i=1;i<tsteps;++i) {
    GetNextIteration(Predator[i-1],Pray[i-1],Predator[i],Pray[i],from_point,to_point);
    Predator[i] = DataExchange(Predator[i],x_points,size,rank);
    Predator[i] = GetBoundData(Predator[i],Predator[i-1],x_points,size,rank);
    Pray[i] = DataExchange(Pray[i],x_points,size,rank);
    Pray[i] = GetBoundData(Pray[i],Pray[i-1],x_points,size,rank);
    //printf("rank is %d\n",rank);
    //Print2dArray(Predator[i-1], x_points+1, y_points);
    //Print2dArray(Predator[i], x_points+1, y_points);
  }

  if (rank == 0)
    SaveResultsToFile(Predator,Pray,rank);

  MPI_Finalize();
  return 0;
}
